package com.stylefeng.guns.biz.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stylefeng.guns.core.redis.RedisService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stylefeng.guns.biz.api.model.PlatformUserInfoVO;
import com.stylefeng.guns.biz.api.service.IPlatformUserInfoService;
import com.stylefeng.guns.core.result.BaseResultVo;
import com.stylefeng.guns.core.result.JsonReturnHelper;

/**
 * pc端登录controller
 * @author zcj
 *
 */
@RequestMapping("/api/pc/unauthorized")
@Controller
public class PcLoginController {

	@Autowired
	private IPlatformUserInfoService iPlateformUserInfoService;

	@Autowired
	private RedisService redisService;


	/**
	 * 用户注册
	 * @return
	 */
	@RequestMapping("/register")
	@ResponseBody
	public BaseResultVo register(PlatformUserInfoVO platformUserInfoVO) {

		Map<String,String> retMap = new HashMap<String,String>();

		try {

			//判断必填字段
			if (StringUtils.isBlank(platformUserInfoVO.getMobile()) || StringUtils.isBlank(platformUserInfoVO.getPassword()) || platformUserInfoVO.getType() == null || StringUtils.isBlank(platformUserInfoVO.getCode())) {
				retMap.put("retCode", "000001");
				retMap.put("retMsg", "手机号/验证码/密码/注册类型不允许为空！");
			} else if (!platformUserInfoVO.getCode().equals(redisService.getValue(platformUserInfoVO.getMobile()))){
				//校验验证码
				retMap.put("retCode", "000002");
				retMap.put("retMsg", "手机验证码不正确！");
			} else {
				platformUserInfoVO.setLevel(0);
				platformUserInfoVO.setStatus(0);

				Map<String, Object> reqParam = new HashMap<>();
				reqParam.put("mobile", platformUserInfoVO.getMobile());
				reqParam.put("type", platformUserInfoVO.getType());
				List<PlatformUserInfoVO> resultList = iPlateformUserInfoService.selectByMap(reqParam);
				if (CollectionUtils.isNotEmpty(resultList)) {
					retMap.put("retCode", "000003");
					retMap.put("retMsg", "用户已经存在!");
				} else {
					iPlateformUserInfoService.insert(platformUserInfoVO);
					retMap.put("retCode", "000000");
					retMap.put("retMsg", "注册成功!");
				}

			}

		} catch (Exception e) {
			retMap.put("retCode", "000001");
			retMap.put("retMsg", "注册失败!");
			e.printStackTrace();
		}

		return JsonReturnHelper.getSuccessWithData(retMap);
	}


	/**
	 * 用户登录
	 * @return
	 */
	@RequestMapping("login")
	@ResponseBody
	public BaseResultVo login(PlatformUserInfoVO platformUserInfoVO) {

		Map<String,Object> retMap = new HashMap<String,Object>(3);

		if (StringUtils.isBlank(platformUserInfoVO.getMobile()) || StringUtils.isBlank(platformUserInfoVO.getPassword()) || platformUserInfoVO.getType() == null) {
			retMap.put("retCode", "000001");
			retMap.put("retMsg", "手机号/密码/用户类型不允许为空!");
			return JsonReturnHelper.getSuccessWithData(retMap);
		}

		Map<String,Object> queryMap= new HashMap<String,Object>();
		queryMap.put("mobile", platformUserInfoVO.getMobile());
		queryMap.put("type", platformUserInfoVO.getType());
		List<PlatformUserInfoVO> result = iPlateformUserInfoService.selectByMap(queryMap);

		if (CollectionUtils.isEmpty(result)) {
			retMap.put("retCode", "000002");
			retMap.put("retMsg", "没有手机号为" + platformUserInfoVO.getMobile() + "，类型为" + (platformUserInfoVO.getType() == 1 ? "广告主" : "流量主") + "的用户！");

		} else {

			String password = result.get(0).getPassword();

			if (platformUserInfoVO.getPassword().equals(password)) {
				retMap.put("retCode", "000000");
				retMap.put("retMsg", "登录成功！");

				result.get(0).setPassword("");
				String token = UUID.randomUUID().toString();
				retMap.put("token", token);
				retMap.put("userInfo", result.get(0));

				redisService.set(token, result.get(0),60*30); //有效时间30分钟
			} else {
				retMap.put("retCode", "000003");
				retMap.put("retMsg", "密码不正确！");
				retMap.put("token", "");
			}

		}

		return JsonReturnHelper.getSuccessWithData(retMap);

	}

	public static void main(String[] args) {
		System.out.println(UUID.randomUUID().toString());
	}

}
